<?php

namespace App\Http\Controllers\Domain;

use App\Models\Account;
use App\Http\Controllers\Controller;
use App\Helper;
use App\Http\Requests\PlayerLoginRequest;
use App\Http\Requests\PlayerRegisterRequest;
use App\Models\Player;
use App\Models\Server;
use Illuminate\Support\Facades\Redirect;


class LoginController extends Controller
{
    public $prefix='domain';

    public function register($domain)
    {
        $server=Server::where('domain',$domain)->first();
        return view($this->prefix.'.register',compact('server'));
    }

    public function registerdo(PlayerRegisterRequest $request,$domain)
    {

        $server=Server::where('domain',$domain)->first();
        if (!$server->Enable) return Redirect::back()->with('msg','服务器过期')->withInput();

        $version=$server->getVersion();
        Helper::setDataBaseConf('mysql_auth',$server,'acore_auth');

        $getAccountSize=Account::where('username',strtoupper($request->get('username')))->count();
        if ($getAccountSize != 0)
        {
            return Redirect::back()->with('msg','该账号已经存在，请更换')->withInput();
        }

        $acc=new Account();
        $acc->username=strtoupper($request->get('username'));
        if (!$server->isOld()){
//        if ($version>20220000){
            $acc->salt=Account::generateSalt();
            $acc->verifier=Account::getVerifier($acc->username,$request->get('password'),$acc->salt);
        }else{
            $acc->sha_pass_hash=strtoupper(sha1(strtoupper($request->get('username')).':'.strtoupper($request->get('password'))));
        }
        //$acc->username=$request->get('mail');
        $acc->expansion=2;

        $acc->save();

        //检查是否存在,重置服务器会导致玩家数据冲突
        Player::where('server_id',$server->id)->where('player_id',$acc->id)->delete();
//        PlayerMoneyLog::where('server_id',$this->id)->delete();

        $player=new Player();
        $player->server_id=$server->id;
        $player->player_id = $acc->id;
        $player->acc=strtoupper($request->get('username'));
        $player->pwd=$request->get('password');
        $player->player_pid=$request->get('pid');
        $player->save();

        return Redirect::to('/login')->with('msg','注册成功，请登录');
    }

    public function login($domain)
    {
        $server=Server::where('domain',$domain)->first();
        return view($this->prefix.'.login',compact('server'));
    }

    public function logindo(PlayerLoginRequest $request,$domain){
        $server=Server::where('domain',$domain)->first();
        if (!$server->Enable) return Redirect::back()->with('msg','服务器过期')->withInput();
        $version=$server->getVersion();
        Helper::setDataBaseConf('mysql_auth',$server,'acore_auth');

        $getAccount=Account::where('username',strtoupper($request->get('username')))->get();

        if (sizeof($getAccount) != 1)
        {
            return Redirect::back()->with('msg','此账号不存在')->withInput();
        }
        $acc=$getAccount[0];
        if (!$server->isOld()){
//        if ($version>20220000){
            $verifier=Account::getVerifier($acc->username,$request->get('password'),$acc->salt);
            if ($acc->verifier!=$verifier)
            {
                return Redirect::back()->with('msg','密码错误')->withInput();
            }
            //清除敏感数据，影响后面转json
            unset($acc->verifier);
            unset($acc->salt);
            unset($acc->session_key);
        }else{
            $pwd=sha1(strtoupper($request->get('username')).':'.strtoupper($request->get('password')));
            if (strtoupper($acc->sha_pass_hash) != strtoupper($pwd))
            {
                return Redirect::back()->with('msg','密码错误')->withInput();
            }
        }

//        dd($acc);
        session(['player' => $acc]);

        //玩家登录检查是否在player表里，如果不在，添加
        $count=Player::where('server_id',$server->id)->where('acc',$acc->username)->count();
        if ($count==0){
            $player=new Player();
            $player->server_id=$server->id;
            $player->player_id = $acc->id;
            $player->acc=$acc->username;
            $player->money=0;
            $player->save();
        }
//        Auth::login($getAccount[0]);
        return Redirect::to('/user/center')->with('msg','登录成功');
        //return Redirect::to('/user/center');
    }

    public function logout()
    {
        session()->forget('player');
        return Redirect::to('/');
    }
}
